package com.lefevre.cms.web.action.feedback;

import com.lefevre.cms.bean.RequestResult;
import com.lefevre.cms.bean.ResultCode;
import com.lefevre.cms.bean.feedback.Feedback;
import com.lefevre.cms.service.feedback.FeedbackService;
import com.lefevre.cms.utils.IpAddress;
import com.lefevre.cms.utils.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 在线留言管理
 *
 * @author Lefevre
 */
@Controller
@RequestMapping("/control/feedback/manage")
public class AdminFeedbackController {

    @Resource
    FeedbackService feedbackService;

    /**
     * 在线留言管理  查看
     */
    @ResponseBody
    @RequestMapping(params = "method=view", method = RequestMethod.GET)
    public String view(@RequestParam Long feedbackId) throws Exception {
        Map<String, String> error = new HashMap<>();
        if (feedbackId != null && feedbackId > 0L) {
            Feedback feedback = feedbackService.findById(feedbackId);
            if (feedback == null) {
                error.put("feedbackId", "在线留言不存在");
            } else {
                if (feedback.getIp() != null && !"".equals(feedback.getIp().trim())) {
                    feedback.setIpAddress(IpAddress.queryAddress(feedback.getIp()));
                }
                return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, feedback));
            }
        } else {
            error.put("feedbackId", "在线留言Id不能为空");
        }
        return JsonUtils.toJSONString(new RequestResult(ResultCode.FAILURE, error));
    }

    /**
     * 在线留言   删除
     *
     * @param feedbackId 在线留言Id
     */
    @ResponseBody
    @RequestMapping(params = "method=delete", method = RequestMethod.POST)
    public String delete(@RequestParam Long feedbackId) throws Exception {
        int i = feedbackService.deleteFeedback(feedbackId);
        return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, null));
    }
}
